<?php
uses('Flay');
//@GEO MAP INTERACTION
 //fetch general config set by admin.
 $disableEditorReviews = cr('GeneralConfig.disable_editor_reviews');
 $distanceLabel = __("miles");
 if(cr('MapConfig.radius_metric') == "km")
 {
 	$ditanceLabel = "K.m";
 }
 $MapConfigModel = cri("MapConfig");
?>

<?php
if(cr("DealConfig.enable_deals"))
{ 
	if(!isset($deals))
	{
		$deals = array();
	}
	if(!isset($showDeals))
	{
		$showDeals = false;
	}
	$dealWiseListingIds = array();
	if(!empty($deals))
	{
		$dealWiseListingIds = Set::combine($deals,'{n}.Deal.id','{n}.Deal.listing_id');
	}
	echo $html->css('/deals/css/deals.css');
}
?>

<div id="listingsIndex" class = "<?php echo $view_type;?>">
<?php
uses("sanitize");
foreach($listings as $listing)
{
	// collect are variables here
	$listingArr = array();
	
	$featuredClass = "";
	if($listing['Listing']['is_featured'] == 1)
	{
		$featuredClass = " featured";	
	}
	$img = $html->image($listing['Listing']['image'], array('alt'=>$listing['Listing']['title'],'thumb'=>cr('Thumbs.listingSmall'), 'height'=>cr('Thumbs.listingSmall.height'), 'width'=>cr('Thumbs.listingSmall.width')));
	$url = $html->url($listing['Listing']['viewUrl']);
	//added for showing/change url of breadcrumbs on basis of category from where it was redirected
	if(cr("ListingConfig.allow_multiple_categories"))
	{
		if(isset($showAsListingCategory) && $showAsListingCategory !== false)
		{
			$url = $url . '/showAsListingCategory:' . $showAsListingCategory;
		}
	}
	//added for showing/change url of breadcrumbs on basis of category from where it was redirected
	$title = $listing['Listing']['title'];
	$addedByLabel = __("Added By");
	$user = $listing['Listing']['poster'];
	$postedTime = $time->timeAgoInwords($listing['Listing']['created'],array('shortString'=>true));
	
	$dealsInfo = "";
	if(cr("DealConfig.enable_deals"))
	{
		if($showDeals && !empty($deals) && !empty($dealWiseListingIds))
		{
			$listingDeal = array_keys($dealWiseListingIds,$listing['Listing']['id']);
			if(!empty($listingDeal))
			{
				$dealsInfo .= "<div class='deal_elem'>";
				foreach ($listingDeal as $dealId)
				{
					if(isset($deals[$dealId]))
					{
						$dealsInfo .= "<span class='deal_elem_offer'> ".__("Deal")." </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;<span>";
						break;
					}
				}
				$dealsInfo .= "</div>";
			}
		}
	}
	
	$listingArr = compact('featuredClass', 'img', 'url', 'title', 'addedByLabel', 'postedTime','dealsInfo');
	
	$_userReviewCount = '<div style="float:left;margin-left:80px">('.$listing['Listing']['review_count'].')</div>';
	$_userRating = $listing['Listing']['userAvg'];
	$_editorRating = $listing['Listing']['editorialAvg'];
	
	$userRatingHtml = $rating->display(array('rating'=>$_userRating,'before_text'=>$_userReviewCount,'tooltip'=>__('User Rating').' '.number_format($_userRating,1),'tooltipClass'=>'user_'.$listing['Listing']['id'].'_'.mt_rand(1,1000)));
	
	$editorRatingHtml = '';
	if($disableEditorReviews != 1)
	{
		$editorRatingHtml = $rating->display(array('rating'=>$_editorRating,'class'=>'editor_rating_star_value', 'parent_class'=>'editor_rating_star_base', 'tooltip'=>__('Editor Rating').' '.number_format($_editorRating,1),'tooltipClass'=>'editor_'.$listing['Listing']['id'].'_'.mt_rand(1,1000)));
	}
	$partialDescription = strip_tags($listing['Listing']['description']);
	$partialDescription = str_replace(array("\n"), "", $partialDescription);
	$partialDescription = Flay::fragment($partialDescription,200);
	$readReviewLink = '';
	if($listing['Listing']['review_count'] > 0)
	{
		$readReviewTitle = __('Read Reviews');
		$readReviewLink = $listing['Listing']['viewUrl'];
		//adding div id of review here so it goes directly to reviews on view page.
		$linkId = "#listing_reviews_".$listing['Listing']['id'];
		if(is_string($readReviewLink))
		{
			$readReviewLink .= "/$linkId";
		}else
		{
			$readReviewLink[] = $linkId;
		}
		$readReviewLink = $html->link($readReviewTitle, $readReviewLink, array('class' => 'read_comments'));
	}
	
	$reviewItTitle = __('Review it');
	$reviewItLink = $html->url('/reviews/add/'.$listing['Listing']['id']);

	$compareListingHtml = '';
	if((!isset($isModuleRender) || $isModuleRender == false) && isset($compareListing) && $compareListing == true)
	{
		//Configure::write('debug', 2);
		// $_compareListingHtml = $this->element('listing_compare_block', array('listing' => $listing), false);
		$_compareListingHtml = $this->element('listing_compare_block', array('listing' => $listing), false);
		
		$compareListingHtml = $form->input('Listing'.$listing['Listing']['id'],array('label'=>'Compare','type'=>'checkbox','id'=>'listing_'.$listing['Listing']['id'],'class'=>'checkListing','onclick'=>'selectListing(this,true);','rel'=>$_compareListingHtml));
	}
	$distanceHtml = "";
	//@GEO MAP INTERACTION
	if(isset($distanceSearch))
	{
		$distanceHtml = __("Distance").' : ';
		$distanceHtml .= $MapConfigModel->getDistanceBetweenPoints($distanceSearch['latLng'],array("lat"=>$listing['Listing']['lat'],"lng"=>$listing['Listing']['lng']));
		$distanceHtml .= " ".$distanceLabel; 
	}
	$listingId = $listing['Listing']['id'];
	$viewPlansLink = null;
	if($session->read("Auth.User.id") && $listing['Listing']['owner_id'] == $session->read("Auth.User.id") && AppUtility::allowPaidListing())
	{
		$viewPlansLink = $html->link(__("Plans"),'/paid_listings/listing_plans/index/'.$listing['Listing']['id'],array("class"=>"plan_link"));
	}
	$params = compact('viewPlansLink','userRatingHtml', 'editorRatingHtml', 'partialDescription', 'readReviewTitle', 'readReviewLink', 'reviewItTitle', 'reviewItLink', 'user', 'addedByLabel', 'compareListingHtml','distanceHtml','listingId');
	$params['listing'] = $listingArr;
	
	echo $this->template('listing_'.$view_type, $params);
	?>
<?php }
?>
</div>
<div class="clear"></div>